<template>
    <el-dialog
        :before-close="resetForm"
        :close-on-click-modal="false"
        :visible.sync="visible"
        append-to-body
        title="编辑字段信息"
        width="560px"
    >
        <div class="edit_page_div">
            <div>
                <el-form ref="form" :model="form" :rules="rules" :show-message="false" label-width="100px" style="float: left;">
                    <el-form-item label="表名" prop="tableName">
                        <el-input v-model="form.tableName" style="width: 400px;"></el-input>
                    </el-form-item>
                    <el-form-item label="字段属性" prop="fieldName">
                        <el-input v-model="form.fieldName" style="width: 400px;"></el-input>
                    </el-form-item>
                    <el-form-item label="字段注解" prop="fieldComment">
                        <el-input v-model="form.fieldComment" style="width: 400px;" type="textarea" rows="3"></el-input>
                    </el-form-item>
                    <el-form-item label="字典编号" prop="dictionaryTypeCode">
                        <el-input v-model="form.dictionaryTypeCode" style="width: 400px;"></el-input>
                    </el-form-item>
                </el-form>
            </div>
            <div class="edit_button" style="margin-top: 10px">
                <el-button v-loading.fullscreen.lock="loading" type="primary" @click="submit">保 存</el-button>
                <el-button type="primary" @click="resetForm">取 消</el-button>
            </div>
        </div>
    </el-dialog>
</template>

<script>
export default {
    name: "changeFieldEdit",
    props: {
        visible: Boolean,
        id: String,
        selectIdList: Array,
    },
    data() {
        return {
            loading: false,
            showUser: false,
            form: {
                tableName: '', //会员编号
                fieldName: '', //会员名称
                fieldComment: '', //会员名称
                dictionaryTypeCode: '', //备注
            },
            rules: {
                tableName: [{required: true, message: '', trigger: 'change'}],
                fieldName: [{required: true, message: '', trigger: 'change'}],
                fieldComment: [{required: true, message: '', trigger: 'change'}],
                dictionaryTypeCode: [{required: false, message: '', trigger: 'change'}],
            },
        }
    },
    watch: {
        visible(newVal) {
            if (newVal) {
                if (this.id) {
                    this.getData();
                }
            }
        }
    },
    methods: {
        getData() {
            this.loading = true;
            this.$axios.get("/CHILD_SYSTEM/baseChangeField/getOneEntity/" + this.id).then(res => {
                this.form = res.data;
            }).finally(() => {
                this.loading = false;
            })
        },
        submit() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.loading = true;
                    this.$axios.post("/CHILD_SYSTEM/baseChangeField/" + (this.id ? "update" : "insert"), this.form).then(res => {
                        this.resetForm();
                        this.$message.success(res.message);
                    }).finally(() => {
                        this.loading = false;
                    })
                } else {
                    this.$message.error('表单填写有误');
                    return false;
                }
            });
        },
        resetForm() {
            this.$refs.form.resetFields();
            this.form = this.$options.data().form;
            this.$emit('update:visible', false);
            this.$emit('refreshList');
        },
    }
}
</script>

<style scoped>

</style>